<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Categories extends MX_Controller {

	var $_table = 'categories';

	function __construct() {
		parent::__construct ();
		if (! isset ( $this->session->userdata ['admin'] )) {
			redirect ( 'admin/login' );
		}

		$_SESSION ['KCFINDER'] = array ();
		$_SESSION ['KCFINDER'] ['disabled'] = false;

		$this->load->library ( 'ckeditor', array ('instanceName' => 'CKEDITOR1', 'basePath' => base_url () . "ckeditor/", 'outPut' => true ) );
		$this->load->model ( 'categories_model' );
		$this->load->model ( '../../gallery/models/gallery_model' );
	}

	function edit_cat() {
		$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		if ($this->input->post ()) {
			if (isset($_FILES['img']) && $_FILES['img']['name']!=''){
				$data_img = array ();
				$image_data = $this->gallery_model->do_upload ();

				$data_img ['file_name'] = ( string ) $image_data ['file_name'];
				$data_img ['type'] = ( string ) $this->input->post ( 'type' );
				$data_img ['path'] = './storage/images';
				$img_id=$this->gallery_model->saveItem ( 'gallery', array ('field' => 'id', 'id' => 0 ), $data_img );

			}else{
				$img_id=0;
			}

			$_id = $this->input->post ( 'id' );
			$data = array ();
			$data ['name'] = ( string ) $this->input->post ( 'name' );
			$data ['name_en'] = ( string ) $this->input->post ( 'name_en' );
			$data ['description'] = ( string ) $this->input->post ( 'description' );
			$data ['description_en'] = ( string ) $this->input->post ( 'description_en' );
			if($img_id != 0){
				$data ['img_id'] = ( int ) $img_id;
			}
			$this->categories_model->saveItem ( 'categories', array ('field' => 'id', 'id' => $_id ), $data );
			redirect ( 'admin/categories/listcategories' );
		}
		$cat = $this->categories_model->getCatDetail ( $id );
		$data ['detail'] = $cat;
		$data ['action'] = 'edit_cat';
		$data ['type'] = 'cat';
		$data ['content'] = 'admin/add';
		$this->load->view ( 'back_end/index', $data );

	}

	function add_cat() {
		if ($this->input->post ()) {
			if (isset($_FILES['img']) && $_FILES['img']['name']!=''){
				$data_img = array ();
				$image_data = $this->gallery_model->do_upload ();
				$data_img ['file_name'] = ( string ) $image_data['file_name'];
				$data_img ['type'] =( string ) $this->input->post ( 'type' );
				$data_img ['path'] = './storage/images';
				$img_id=$this->gallery_model->saveItem( 'gallery', array ('field' => 'id', 'id' => 0 ), $data_img );
			}else{
				$img_id=0;
			}
			$data = array ();
			$data ['name'] = ( string ) $this->input->post ( 'name' );
			$data ['name_en'] = ( string ) $this->input->post ( 'name_en' );
			$data ['description'] = ( string ) $this->input->post ( 'description' );
			$data ['description_en'] = ( string ) $this->input->post ( 'description_en' );
			$data ['img_id'] = ( int ) $img_id;
			$this->categories_model->saveItem ( 'categories', array ('id' => 0 ), $data );
			redirect ( 'admin/categories/listcategories' );
		}
		$data ['mode'] = 'add';
		$data ['action'] = 'add_cat';
		$data ['type'] = 'cat';
		$data ['content'] = 'admin/add';
		$this->load->view ( 'back_end/index', $data );
	}

	function listcategories() {
		$catid = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$data ['categories'] = $this->categories_model->getCategoriesList ();
		$data ['content'] = 'admin/list';
		$this->load->view ( 'back_end/index', $data );
	}


	function del_cat($id = NULL) {
		if ($id == NULL) {
			$id = ( int ) $this->uri->segment ( 4 );
		}
		$this->categories_model->del_cat ( $id );
		redirect ( 'admin/categories/listcategories' );
	}
}